package com.api.gbhg.huangwm.action;

import com.api.gbhg.huangwm.dao.ReimbursementListDao;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Huangwm
 * @Date 2022-05-04
 * @Version 1.0
 * @Description TODO 明细报销会计审核数据写入
 **/
public class RecordAccountingDetail implements Action {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/action/RecordAccountingDetail",false);
    private ReimbursementListDao dao = new ReimbursementListDao();
    public String zy_dbname = "";
    public String getzy_dbname(){
        return zy_dbname;
    }
    public void setzy_dbname(String zy_dbname){
        this.zy_dbname = zy_dbname;
    }

    @Override
    public String execute(RequestInfo requestInfo) {
        if(this.zy_dbname.isEmpty() || this.zy_dbname.equals("")){
            this.zy_dbname = "null";
        }
        String requestid = requestInfo.getRequestid();
        dao.runBefore(requestid);
        User user = requestInfo.getRequestManager().getUser();
        String table = requestInfo.getRequestManager().getBillTableName();
        int cnodeid = requestInfo.getRequestManager().getNextNodeid();
        String cnodetype = requestInfo.getRequestManager().getNextNodetype();
        int billid = requestInfo.getRequestManager().getBillid();
        String operDate = requestInfo.getRequestManager().getCurrentDate();
        logTool.writeLog("  table="+table+" billid"+billid+" requestid="+requestid+" cnodeid="+cnodeid+" cnodetype="+cnodetype);

        List<List> mainDatas = new ArrayList<>();
        List<List> dt1Datas = new ArrayList<>();
        List<List> dt2Datas = new ArrayList<>();

        try{
            //获取主表信息
            String mainSql = "select id,requestid,jbr,jbks,jbbm,sqrq,lcbh from "+table+" where requestid="+requestid;
            RecordSet rs1 = new RecordSet();
            rs1.execute(mainSql);
            rs1.next();
            String jbr = rs1.getString("jbr");
            String jbks = rs1.getString("jbks");
            String jbbm = rs1.getString("jbbm");
            String sqrq = rs1.getString("sqrq");
            String lcbh = rs1.getString("lcbh");
            Object[] mainParams = new Object[]{requestid,requestid,jbr,jbks,jbbm,sqrq,lcbh,user.getUID(),operDate,cnodeid,cnodetype};
            List<String> maindata = new ArrayList<>();
            for (Object obj : mainParams){
                maindata.add(obj.toString());
            }
            mainDatas.add(maindata);

            RecordSet rs2 = new RecordSet();
            rs2.execute("select dt.id,"+zy_dbname+",ysxm,zflx,bmjjzcfl,zfjjzcfl,ysly,zcqd,zcgnkm,bxkmdm,zfl,sqbxje,tybxje\n" +
                            ",kmjxj,dygnkmbid,fzysrfl,zcdfczbkxm,hpssjid \n"+
                            "from "+table+"_dt1 dt \n"+
                            "left join uf_gxmdygnkm on uf_gxmdygnkm.id=dt.xzkmdm \n" +
                            "left join uf_srysfl on dt.fzysrfl=uf_srysfl.id \n" +
                            "left join uf_fzysrxm on dt.zcdfczbkxm=uf_fzysrxm.id \n" +
                            "where mainid="+billid);
            while(rs2.next()){
                String dt1_id = rs2.getString("id");
                String dt1_zy = rs2.getString(zy_dbname);
                String dt1_ysxm = rs2.getString("ysxm");
                String dt1_zflx = rs2.getString("zflx");
                String dt1_bmjjzcfl = rs2.getString("bmjjzcfl");
                String dt1_zfjjzcfl = rs2.getString("zfjjzcfl");
                String dt1_ysly = rs2.getString("ysly");
                String dt1_zcqd = rs2.getString("zcqd");
                String dt1_zcgnkm = rs2.getString("zcgnkm");
                String dt1_bxkmdm = rs2.getString("bxkmdm");
                String dt1_zfl = rs2.getString("zfl");
                String dt1_sqbxje = rs2.getString("sqbxje").equals("")?"0":rs2.getString("sqbxje");
                String dt1_tybxje = rs2.getString("tybxje").equals("")?"0":rs2.getString("tybxje");
                String kmjxj = Util.null2String(rs2.getString("kmjxj"));//科目级下级 0-科目级 1-科目下级
                String dygnkmbid = Util.null2String(rs2.getString("hpssjid")).equals("")?"":rs2.getString("hpssjid").split("_")[1];//核批的财政收入表id
                String fzysrfl = Util.null2String(rs2.getString("fzysrfl"));//非中央收入分类
                String zcdfczbkxm = Util.null2String(rs2.getString("zcdfczbkxm"));//地方财政拨款项目
                Object[] dt1Params = new Object[]{
                        requestid,dt1_id,dt1_zy,dt1_zflx,dt1_ysxm,dt1_bmjjzcfl,dt1_zfjjzcfl,
                        dt1_bxkmdm,dt1_zcqd,dt1_ysly,dt1_zcgnkm,dt1_zfl,dt1_sqbxje,dt1_tybxje,
                        kmjxj,dygnkmbid,fzysrfl,zcdfczbkxm
                };
                List<String> dt1data = new ArrayList<>();
                for (Object obj : dt1Params){
                    dt1data.add(obj.toString().isEmpty()?"":obj.toString());
                }
                dt1Datas.add(dt1data);
            }
            RecordSet rs3 = new RecordSet();
            rs3.execute("select id,zzlx,zffs,fkyx,fkzh,zfje,tyzfje,gwkskje from "+table+"_dt2 where mainid="+billid);
            while(rs3.next()){
                String dt2_id = rs3.getString("id");
                String dt2_zzlx = rs3.getString("zzlx");
                String dt2_zffs = rs3.getString("zffs");
                String dt2_fkyx = rs3.getString("fkyx");
                String dt2_fkzh = rs3.getString("fkzh");
                String dt2_zfje = rs3.getString("zfje").equals("")?"0":rs3.getString("zfje");
                String dt2_tyzfje = rs3.getString("tyzfje").equals("")?"0":rs3.getString("tyzfje");
                String dt2_gwkskje = rs3.getString("gwkskje").equals("")?"0":rs3.getString("gwkskje");
                Object[] dt2Params = new Object[]{requestid,dt2_id,dt2_zzlx,dt2_zffs,dt2_fkyx,dt2_fkzh,dt2_zfje,dt2_tyzfje,dt2_gwkskje};
                List<String> dt2data = new ArrayList<>();
                for (Object obj : dt2Params){
                    dt2data.add(obj.toString().isEmpty()?"":obj.toString());
                }
                dt2Datas.add(dt2data);
            }

            RecordSet rst = new RecordSet();
            boolean main_result=true,dt1_result=true,dt2_result=true;
            try {
                logTool.writeLog("mainDatas.size="+mainDatas.size());
                logTool.writeLog("dt1Datas.size="+dt1Datas.size());
                logTool.writeLog("dt2Datas.size="+dt2Datas.size());
                main_result = rst.executeBatchSql(dao.sql1,mainDatas);
                dt1_result = rst.executeBatchSql(dao.sql2,dt1Datas);
                dt2_result = rst.executeBatchSql(dao.sql3,dt2Datas);
                logTool.writeLog("main_result="+main_result+"  dt1_result="+dt1_result+"  dt2_result="+dt2_result);
                if(!main_result || !dt1_result || !dt2_result){
                    requestInfo.getRequestManager().setMessageid(requestid);
                    requestInfo.getRequestManager().setMessagecontent("写入报销会计数据执行SQL失败 "+"main_result="+main_result+"  dt1_result="+dt1_result+"  dt2_result="+dt2_result);
                    return FAILURE_AND_CONTINUE;
                }else{
                    logTool.writeLog("同步"+table+"结束");
                    dao.runAfter(requestid);
                    new RecordSet().execute("update uf_bxjl_kj_dt1 set dw=dlhsdw from uf_jingfei_version where uf_bxjl_kj_dt1.ysxm=uf_jingfei_version.lcjlsqjlid\n" +
                            "and uf_bxjl_kj_dt1.lcid="+requestid);
                    return SUCCESS;
                }
            }catch (Exception e){
                requestInfo.getRequestManager().setMessageid(requestid);
                requestInfo.getRequestManager().setMessagecontent("写入报销会计数据异常 "+e.getMessage());
                return FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            requestInfo.getRequestManager().setMessageid(requestid);
            requestInfo.getRequestManager().setMessagecontent("获取报销流程数据异常 "+e.getMessage());
            return FAILURE_AND_CONTINUE;
        }
    }
}
